var BoxChoose = (function () {
    function throttle(func, interval) {
        var allowExec = true;
        var attrs = [];
        var timer = 0;
        return function () {
            attrs = Array(0).slice.apply(arguments);
            if (allowExec) {
                func.apply(this, attrs);
                allowExec = false;
                return;
            }
            if (!timer) {
                timer = setTimeout(function () {
                    allowExec = true;
                    timer = 0;
                }, interval || 16);
            }
        };
    }
    // 显示的盒子样式
    var box_attr = {
        style: "border:1px dashed #ff0000;background:rgba(255,255,255,0.6);pointer-events:none;"
    };
    // 是否已经开始绘制
    var isStart = false;
    // 起始点
    var startPoint = {
        x: 0,
        y: 0
    };
    // 末尾点
    var endPoint = {
        x: 0,
        y: 0
    };
    var box = document.createElement("div");
    box.style.cssText = box_attr.style;
    box.style.position = "fixed";
    function start(x, y) {
        startPoint.x = x;
        startPoint.y = y;
        box.style.left = x + "px";
        box.style.top = y + "px";
        box.style.width = 0 + "px";
        box.style.height = 0 + "px";
        if (!isStart) {
            document.body.appendChild(box);
        }
        isStart = true;
    }
    function move(x, y) {
        if (!isStart) {
            return;
        }
        endPoint.x = x;
        endPoint.y = y;
        box.style.left = startPoint.x + "px";
        box.style.top = startPoint.y + "px";
        if (startPoint.x > endPoint.x) {
            box.style.left = endPoint.x + "px";
        }
        if (startPoint.y > endPoint.y) {
            box.style.top = endPoint.y + "px";
        }
        box.style.height = Math.abs(y - startPoint.y) + "px";
        box.style.width = Math.abs(x - startPoint.x) + "px";
    }
    function end() {
        box.style.left = 0 + "px";
        box.style.top = 0 + "px";
        box.style.width = 0 + "px";
        box.style.height = 0 + "px";
        // 起始点
        startPoint = {
            x: 0,
            y: 0
        };
        // 末尾点
        endPoint = {
            x: 0,
            y: 0
        };
        if(isStart){
            document.body.removeChild(box);
        }
        isStart = false;
    }
    return {
        start: start,
        move: throttle(move, 16),
        end: end
    };
}());
